package com.iqiyi.pps.epg.api.utils.puyu;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.*;

//文件为通过jar包反编译生成
public class HttpSignatureUtil {

    public static final String NAME_VALUE_DELIMETER = "=";
    public static final String ID_SIGN_DELIMETER = ":";
    public static final String PAIR_DELIMETER = "&";
    public static final String DEFAULT_CARHSET = "UTF-8";

    public static String createToken(String clientId, String key, Map parameters) {
        String data;
        if (isEmpty(clientId))
            throw new IllegalArgumentException("\u5BA2\u6237\u7AEFID\u4E0D\u53EF\u4E3A\u7A7A");
        if (isEmpty(key))
            throw new IllegalArgumentException("\u5BA2\u6237\u7AEF\u5BC6\u94A5\u4E0D\u53EF\u4E3A\u7A7A");
        if (parameters == null || parameters.isEmpty())
            throw new IllegalArgumentException("\u8BF7\u6C42\u6570\u636E\u4E0D\u53EF\u4E3A\u7A7A");
        data = join(parameters);
        String original;
        try {
            byte cryptograph[] = TripleDESUtil.encrypt(key, data);
            String sign = DigestUtils.md5Hex(cryptograph);
            original = (new StringBuilder(String.valueOf(clientId))).append(ID_SIGN_DELIMETER).append(sign).toString();
            return Base64.encodeBase64String(original.getBytes(DEFAULT_CARHSET));
        } catch (Exception exception) {
            throw new RuntimeException("\u8BA1\u7B97\u8BA4\u8BC1\u4FE1\u606F\u5931\u8D25", exception);
        }

    }

    public static String[] splitToken(String token) {
        String decoded = new String(Base64.decodeBase64(token));
        return decoded.split(ID_SIGN_DELIMETER);
    }

    public static boolean validate(String key, Map parameters, String sign) {
        String data = join(parameters);
        String localSign;
        try {
            byte cryptograph[] = TripleDESUtil.encrypt(key, data);
            localSign = DigestUtils.md5Hex(cryptograph);
            return sign.equalsIgnoreCase(localSign);
        } catch (Exception exception) {
            return false;
        }

    }

    public static String join(Map parameters) {
        List keys = new ArrayList();
        keys.addAll(parameters.keySet());
        Collections.sort(keys, new Comparator() {

            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }

            public int compare(Object obj, Object obj1) {
                return compare((String) obj, (String) obj1);
            }

        });
        StringBuffer pairs = new StringBuffer();
        String key;
        for (Iterator iterator = keys.iterator(); iterator.hasNext(); pairs.append(key).append(NAME_VALUE_DELIMETER).append(parameters.get(key)).append(PAIR_DELIMETER))
            key = (String) iterator.next();

        return removeEnd(pairs.toString(), PAIR_DELIMETER);
    }

    private static String removeEnd(String target, String remove) {
        if (isEmpty(target) || isEmpty(remove))
            return target;
        if (target.endsWith(remove))
            return target.substring(0, target.length() - remove.length());
        else
            return target;
    }

    private static boolean isEmpty(String target) {
        return target == null || target.length() == 0;
    }
}
